/*
 * The ELepHant Reasoner
 *
 * Copyright (C) Baris Sertkaya (sertkaya.baris@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <assert.h>

#include "set.h"
#include "../hashing/hash_table.h"

Set* set_create(int size) {
	Set* s = calloc(1, sizeof(Set));
	assert(s != NULL);
	s->hash_table = hash_table_create(size);
	s->size = 0;
	return s;
}

char set_add(void* e, Set* s) {
	++s->size;
	return hash_table_insert(s->hash_table, e);
}

/**
 * TODO
 */
char set_remove(void* e, Set* s) {

	--s->size;
	return 0;
}

int set_free(Set* s) {
	int freed_bytes = hash_table_free(s->hash_table);
	free(s);

	return (freed_bytes +  sizeof(Set));
}

char set_contains(void* e, Set* s) {
	return hash_table_contains(s->hash_table, e);
}

/**
 * TODO:
 */
void set_to_list(Set* s, void** l) {
	int i, j, list_length = 0;
	for (i = 0; i < s->hash_table->bucket_count; ++i)
		if (s->hash_table->buckets[i] != NULL)
			for (j = 0; j < s->hash_table->chain_sizes[i]; ++j) {
				l[list_length] = s->hash_table->buckets[i][j];
				++list_length;
			}
	return;
}
